import validator from 'validator';
import { IUserDocument } from "../models/user"
import { error } from 'console';

export interface IRegisterInput extends Partial<IUserDocument> {
    confirmPassword?: string
}
export interface IRegisterInputValidateResult {
    errors: IRegisterInput,
    valid: boolean
}

export const validateRegisterInput = function (username: string, password: string, confirmPassword: string, email: string): any {
    let errors: IRegisterInput = {};
    if (username === undefined || validator.isEmpty(username)) {
        errors.username = "用户名不能为空";
    }
    if (password === undefined || validator.isEmpty(password)) {
        errors.password = "密码不能为空";
    }
    if (confirmPassword === undefined || validator.isEmpty(confirmPassword)) {
        errors.confirmPassword = "确认密码不能为空";
    }
    if (!validator.equals(password, confirmPassword)){
        errors.confirmPassword = "确认密码和密码不相等";
    }
    if (email === undefined || validator.isEmpty(email)){
        errors.email = "邮箱不能为空";
    }
    if (!validator.isEmail(email)) {
        errors.email = "邮箱格式必须合法";
    }
    return {
        errors:Object.keys(errors).length?Object.values(errors).join(";"):"",
        valid: Object.keys(errors).length === 0
    };
}